#include "statement.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef int (*case_func)();

void test_case(const char* description, case_func cf) {
	int result = cf();
	printf("%s: %s\n", description, result ? "failed" : "ok");
}


int create_and_match() {
	Statement* stmt; 	Statement* mask; 
	int result = -1;
	stmt =  stmt_new("a", "b", "c", 1);
	if ( !stmt ) goto cleanup1;
	mask = stmt_new("a", "b", "c", 1);
	if ( !mask ) goto cleanup2;
	
	if (stmt_match(mask, stmt) )
		result = 0;
	else
		result = 1;
cleanup2:
	stmt_delete(mask);
cleanup1:
	stmt_delete(stmt);
	return result;
}

int create_twice_and_check() {
	Statement* stmt1, *stmt2;
	int result  = -1;
	stmt1 = stmt_new("a", "b", "c", 1);
	if ( !stmt1 ) goto cleanup1;
	
	stmt2 = stmt_new("a", "x", "y", 1);
	if ( !stmt2 ) goto cleanup2;
	
	stmt_prepend(&stmt1, stmt2);
	if (stmt1 == stmt2) {
		result = 0;
		goto cleanup1;
	} 

cleanup2:
	stmt_delete(stmt2);

cleanup1:
	stmt_delete(stmt1);
	return result;

}

int create_twice_and_count() {
	Statement* stmt1, *stmt2;
	int result  = -1;
	stmt1 = stmt_new("a", "b", "c", 1);
	if ( !stmt1 ) goto cleanup1;
	
	stmt2 = stmt_new("a", "x", "y", 1);
	if ( !stmt2 ) goto cleanup2;
	
	stmt_prepend(&stmt1, stmt2);
	if ( stmt_count(stmt1)  == 2 ) {
		result = 0;
		goto cleanup1;
	}
cleanup2:
	stmt_delete(stmt2);

cleanup1:
	stmt_delete(stmt1);
	return result;

}

int main() {
	test_case("Create and Match", create_and_match);
	test_case("Create x2 and Check", create_twice_and_check);
	test_case("Create x2", create_twice_and_count);
	return 0;
}
